package com.tarena.lbs.content.dao.repository.impl;

import com.tarena.lbs.content.dao.mapper.CategoryMapper;
import com.tarena.lbs.content.dao.repository.CategoryRepository;
import com.tarena.lbs.content.pojo.param.ArticleCategoryParam;
import com.tarena.lbs.content.pojo.po.ArticleCategory;
import com.tarena.lbs.content.pojo.vo.ArticleCategoryVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Repository
@RequiredArgsConstructor
public class CategoryRepositoryImpl implements CategoryRepository {
    private final CategoryMapper categoryMapper;
    @Override
    public List<ArticleCategoryVO> getAllArticleCategory(){
        List<ArticleCategory> defaultList = categoryMapper.selectList(null);
        List<ArticleCategoryVO> resultList = new ArrayList<>();
        defaultList.forEach(po -> {
            ArticleCategoryVO vo = new ArticleCategoryVO();
            BeanUtils.copyProperties(po,vo);
            resultList.add(vo);
        });
        return resultList.stream().sorted((Comparator.comparing(ArticleCategoryVO::getId))).toList();
    }
    @Override
    public void addArticleCategory(ArticleCategory category) {
        categoryMapper.insert(category);
    }
    @Override
    public void updateArticleCategory(ArticleCategory category) {
        categoryMapper.updateById(category);
    }
}
